home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / fntool / process.c < prev    next >
Text File  |  1993-12-06  |  5KB  |  198 lines

  1. #include "fntool.h"
  2. #ifdef __GNUC__
  3. #include <sys/file.h>
  4. #endif
  5. #ifdef __TURBOC__
  6. #define  F_OK    0
  7. #endif
  8.  
  9. static void loadfont(char *name)
  10. {
  11.     char *p;
  12.     int  type = opt.intype;
  13.  
  14.     if(type == UNK) {
  15.         if((p = strrchr(name,'.')) == NULL) type = FNT;
  16.         else if(strmatch(p,".bdf")) type = BDF;
  17.         else if(strmatch(p,".fna")) type = FNA;
  18.         else if(strmatch(p,".fnt")) type = FNT;
  19.         else if(strmatch(p,".c"))    type = _C_;
  20.         else type = FNT;
  21.     }
  22.     openinput(name,(type == FNT) ? "rb" : "r");
  23.     if(opt.verbose) {
  24.         printf("  reading \"%s\"...\t",inname);
  25.         fflush(stdout);
  26.     }
  27.     memset(&fnt,0,sizeof(font));
  28.     notes[0] = '\0';
  29.     strcpy(fnt.name,name);
  30.     if((p = strrchr(fnt.name,'.')) != NULL) *p = '\0';
  31.     switch(type) {
  32.         case BDF: readbdf(); break;
  33.         case FNA: readfna(); break;
  34.         case FNT: readfnt(); break;
  35.         case _C_: readc();     break;
  36.     }
  37.     closeinput();
  38.     computewidth();
  39. }
  40.  
  41. static void convertfont(void)
  42. {
  43.     if(opt.verbose) {
  44.         printf("processing...\t");
  45.         fflush(stdout);
  46.     }
  47.     if(opt.do_prop2fix) cvtprop2fix();
  48.     if(opt.do_fix2prop) cvtfix2prop();
  49.     if(opt.do_centerwdt) centerwidth();
  50.     if(opt.do_centerhgt) centerheight();
  51. }
  52.  
  53. static void savefont(void)
  54. {
  55.     char *p;
  56.  
  57.     if((p = strrchr(fnt.name,'.')) != NULL) *p = '\0';
  58.     switch(opt.outtype) {
  59.         case FNA: strcat(fnt.name,".fna"); break;
  60.         case FNT: strcat(fnt.name,".fnt"); break;
  61.         case _C_: strcat(fnt.name,".c");   break;
  62.     }
  63.     openoutput(fnt.name,(opt.outtype == FNT) ? "wb" : "w");
  64.     if(opt.verbose) {
  65.         printf("writing \"%s\"...\t",outname);
  66.         fflush(stdout);
  67.     }
  68.     if((p = strrchr(fnt.name,'\\')) != NULL) strcpy(fnt.name,++p);
  69.     if((p = strrchr(fnt.name,'/'))  != NULL) strcpy(fnt.name,++p);
  70.     if((p = strrchr(fnt.name,':'))  != NULL) strcpy(fnt.name,++p);
  71.     for(p = fnt.name; *p != '\0'; p++) *p = tolower(*p);
  72.     switch(opt.outtype) {
  73.         case FNA: writefna(); break;
  74.         case FNT: writefnt(); break;
  75.         case _C_: writec();      break;
  76.     }
  77.     closeoutput();
  78. }
  79.  
  80. static void enforceoptions(void)
  81. {
  82.     char *pp,*np;
  83.     chr  *cp,*nxt;
  84.  
  85.     if(*(pp = opt.namepattern) != '\0') {
  86.         char unique = '\0';
  87.         for( ; ; ) {
  88.         for(np = fnt.name; *pp != '\0'; pp++) {
  89.             if(*pp == '%') switch(*++pp) {
  90.               case '%':
  91.             *np++ = '%';
  92.             break;
  93.               case 'W': case 'w':
  94.             sprintf(np,"%d",fnt.avgwidth);
  95.             np += strlen(np);
  96.             break;
  97.               case 'H': case 'h':
  98.             sprintf(np,"%d",fnt.height);
  99.             np += strlen(np);
  100.             break;
  101.               case 'G': case 'g':
  102.             if(fnt.weight[0] != '\0') *np++ = fnt.weight[0];
  103.             break;
  104.               case 'S': case 's':
  105.             if(fnt.slant[0] != '\0') *np++ = fnt.slant[0];
  106.             break;
  107.               case 'U': case 'u':
  108.             if(unique != '\0') {
  109.                 *np++ = unique;
  110.                 unique++;
  111.                 if((unique == 'b') || (unique == 'i')) unique++;
  112.                 break;
  113.             }
  114.             unique = 'a';
  115.             break;
  116.             }
  117.             else *np++ = *pp;
  118.         }
  119.         *np = '\0';
  120.         if(unique == '\0') break;
  121.         if((pp = strrchr(fnt.name,'.')) != NULL) *pp = '\0';
  122.         switch(opt.outtype) {
  123.             case FNA: strcat(fnt.name,".fna"); break;
  124.             case FNT: strcat(fnt.name,".fnt"); break;
  125.             case _C_: strcat(fnt.name,".c");   break;
  126.         }
  127.         if(access(fnt.name,F_OK) != 0) break;
  128.         pp = opt.namepattern;
  129.         }
  130.     }
  131.     if(opt.family[0] != '\0') strcpy(fnt.family,opt.family);
  132.     if(fnt.minchar < opt.minchar) {
  133.         for(cp = fnt.chars; cp != NULL; cp = nxt) {
  134.         nxt = cp->next;
  135.         if(cp->code == opt.minchar) {
  136.             fnt.minchar = cp->code;
  137.             fnt.chars = cp;
  138.             break;
  139.         }
  140.         free(cp->bmp);
  141.         free(cp);
  142.         }
  143.         computewidth();
  144.     }
  145.     if(fnt.maxchar > opt.maxchar) {
  146.         for(cp = fnt.chars; cp != NULL; cp = nxt) {
  147.         nxt = cp->next;
  148.         if(cp->code == opt.maxchar) {
  149.             fnt.maxchar = cp->code;
  150.             cp->next = NULL;
  151.         }
  152.         if(cp->code > opt.maxchar) {
  153.             free(cp->bmp);
  154.             free(cp);
  155.         }
  156.         }
  157.         computewidth();
  158.     }
  159. }
  160.  
  161. static void destroyfont(void)
  162. {
  163.     chr *cp,*nxt;
  164.  
  165.     for(cp = fnt.chars; cp != NULL; cp = nxt) {
  166.         nxt = cp->next;
  167.         free(cp->bmp);
  168.         free(cp);
  169.     }
  170.     memset(&fnt,0,sizeof(font));
  171.     notes[0] = '\0';
  172. }
  173.  
  174. void processfonts(int argc,char **argv)
  175. {
  176.     if(opt.outtype == DIR) {
  177.         if(opt.verbose)
  178.         printf("  creating font directory: \"%s\"\n",opt.namepattern);
  179.         openoutput(opt.namepattern,"wb");
  180.         writedir(argc,argv);
  181.         closeoutput();
  182.     }
  183.     else while(--argc >= 0) {
  184.         loadfont(*argv++);
  185.         convertfont();
  186.         enforceoptions();
  187.         if(opt.do_show) showfont();
  188.         if(opt.do_edit) editfont();
  189.         savefont();
  190.         destroyfont();
  191.         if(opt.verbose) {
  192.         printf("done\n");
  193.         fflush(stdout);
  194.         }
  195.     }
  196. }
  197.  
  198.